using System;
using System.Data;
using System.Collections;
using System.Collections.Specialized;

using Actions;
using Actions.Databases;

namespace Actions.Security
{	
	/// <summary>
	/// Role - describe user role in process
	/// </summary>
	public class Role
	{
		private	string _name;
		private string _title;		
		private ArrayList _usecases;
		
		public string 		name
		{
			get
			{
				return _name;	
			}
		}				
		public string 		title
		{
			get
			{
				return _title;	
			}
		}				
		
		public Role()
		{
			_name = "Anonymous";
			_usecases = new ArrayList();			
			_usecases.Add(new Usecase("Start"));
			_usecases.Add(new Usecase("403"));
			_usecases.Add(new Usecase("404"));
		}
		
		public Role(string dbid, string roleName)
		{			
			_name = roleName;
			_title = roleName;
			_usecases = new ArrayList();
						
			QueryResult qr = Manager.i.db(dbid)
				.query(string.Format("SELECT * FROM _roles WHERE id='{0}' AND enable", _name));			

			// main primary functions
			_usecases.Add(new Usecase("403"));
			_usecases.Add(new Usecase("404"));
			
			if(qr.res.Count == 1) {
				_title = qr.got(0, "title");
				
				string[] uc = qr.got(0, "usecases").Split(',');
        		foreach (string uci in uc) {
         			_usecases.Add(new Usecase(uci));
				}
			}
		}
		
		public bool checkUsecase(Usecase usecase)
		{
			bool result = false;
			
			foreach(Usecase u in _usecases){
				if(usecase == u) {
					result = true;
					break;
				}
			}			
			return result;
		}		
	}
}
